{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0b187403",
   "metadata": {},
   "source": [
    "# Python 使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "40617fd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "67ea11dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "import requests\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.api账号、通行证\n",
    "API_Key = \"FR1bg4fNm_Vl53EVeHYjOQQTv6_gm_vP\"\n",
    "API_Secret = \"78fJkFZIfdFq5EGrvQ63PGgX5QwddSsw\"\n",
    "\n",
    "# 3.请求方法：POST\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "     \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F8434dd571149b56667991898e2004376212d8267169b3-P2VD0B_fw236&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649382239&t=6f50876f42b564781b9bc484a24600aa\",\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6ebe5201",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "823a2454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648468826,d29fcd45-fff4-4507-b8bc-c563cd91a928\",\"time_used\":233,\"faces\":[{\"face_token\":\"5df3deccb38e06baa38ac12af7d8968e\",\"face_rectangle\":{\"top\":97,\"left\":29,\"width\":173,\"height\":173}}],\"image_id\":\"8a+bXqEezn5PpUuMyXz9xw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "399a6214",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648468826,d29fcd45-fff4-4507-b8bc-c563cd91a928',\n",
       " 'time_used': 233,\n",
       " 'faces': [{'face_token': '5df3deccb38e06baa38ac12af7d8968e',\n",
       "   'face_rectangle': {'top': 97, 'left': 29, 'width': 173, 'height': 173}}],\n",
       " 'image_id': '8a+bXqEezn5PpUuMyXz9xw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3f618b50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5df3deccb38e06baa38ac12af7d8968e'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b2560fe",
   "metadata": {},
   "source": [
    "# 人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "22af7732",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2.api账号、通行证\n",
    "API_Key = \"FR1bg4fNm_Vl53EVeHYjOQQTv6_gm_vP\"\n",
    "API_Secret = \"78fJkFZIfdFq5EGrvQ63PGgX5QwddSsw\"\n",
    "\n",
    "# 3.请求方法：POST\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "     \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":\"5df3deccb38e06baa38ac12af7d8968e\",\n",
    "    \"return_attributes\":\"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus\"\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0917bdcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "882fb61d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"time_used\": 135, \"request_id\": \"1648468914,d763cb62-288e-47f8-a22a-96725f287a1d\", \"faces\": [{\"attributes\": {\"emotion\": {\"sadness\": 0.004, \"neutral\": 99.852, \"disgust\": 0.0, \"anger\": 0.0, \"surprise\": 0.001, \"fear\": 0.0, \"happiness\": 0.143}, \"beauty\": {\"female_score\": 83.177, \"male_score\": 81.705}, \"gender\": {\"value\": \"Female\"}, \"age\": {\"value\": 28}, \"mouthstatus\": {\"close\": 99.999, \"surgical_mask_or_respirator\": 0.0, \"open\": 0.001, \"other_occlusion\": 0.0}, \"glass\": {\"value\": \"None\"}, \"skinstatus\": {\"dark_circle\": 3.346, \"stain\": 5.155, \"acne\": 1.008, \"health\": 38.454}, \"headpose\": {\"yaw_angle\": 1.0588919, \"pitch_angle\": 12.025846, \"roll_angle\": 0.89225835}, \"blur\": {\"blurness\": {\"threshold\": 50.0, \"value\": 0.391}, \"motionblur\": {\"threshold\": 50.0, \"value\": 0.391}, \"gaussianblur\": {\"threshold\": 50.0, \"value\": 0.391}}, \"smile\": {\"threshold\": 50.0, \"value\": 0.009}, \"eyestatus\": {\"left_eye_status\": {\"normal_glass_eye_open\": 0.054, \"no_glass_eye_close\": 0.0, \"occlusion\": 0.0, \"no_glass_eye_open\": 99.946, \"normal_glass_eye_close\": 0.0, \"dark_glasses\": 0.0}, \"right_eye_status\": {\"normal_glass_eye_open\": 0.042, \"no_glass_eye_close\": 0.0, \"occlusion\": 0.0, \"no_glass_eye_open\": 99.958, \"normal_glass_eye_close\": 0.0, \"dark_glasses\": 0.0}}, \"facequality\": {\"threshold\": 70.1, \"value\": 90.026}, \"eyegaze\": {\"right_eye_gaze\": {\"position_x_coordinate\": 0.462, \"vector_z_component\": 0.944, \"vector_x_component\": -0.072, \"vector_y_component\": -0.321, \"position_y_coordinate\": 0.406}, \"left_eye_gaze\": {\"position_x_coordinate\": 0.483, \"vector_z_component\": 0.955, \"vector_x_component\": -0.027, \"vector_y_component\": -0.294, \"position_y_coordinate\": 0.401}}}, \"face_rectangle\": {\"width\": 173, \"top\": 97, \"left\": 29, \"height\": 173}, \"face_token\": \"5df3deccb38e06baa38ac12af7d8968e\"}]}'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5a500110",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 135,\n",
       " 'request_id': '1648468914,d763cb62-288e-47f8-a22a-96725f287a1d',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.004,\n",
       "     'neutral': 99.852,\n",
       "     'disgust': 0.0,\n",
       "     'anger': 0.0,\n",
       "     'surprise': 0.001,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.143},\n",
       "    'beauty': {'female_score': 83.177, 'male_score': 81.705},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 28},\n",
       "    'mouthstatus': {'close': 99.999,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.001,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 3.346,\n",
       "     'stain': 5.155,\n",
       "     'acne': 1.008,\n",
       "     'health': 38.454},\n",
       "    'headpose': {'yaw_angle': 1.0588919,\n",
       "     'pitch_angle': 12.025846,\n",
       "     'roll_angle': 0.89225835},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.391},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.391},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.391}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.009},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.054,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.946,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.042,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.958,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 90.026},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.462,\n",
       "      'vector_z_component': 0.944,\n",
       "      'vector_x_component': -0.072,\n",
       "      'vector_y_component': -0.321,\n",
       "      'position_y_coordinate': 0.406},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.483,\n",
       "      'vector_z_component': 0.955,\n",
       "      'vector_x_component': -0.027,\n",
       "      'vector_y_component': -0.294,\n",
       "      'position_y_coordinate': 0.401}}},\n",
       "   'face_rectangle': {'width': 173, 'top': 97, 'left': 29, 'height': 173},\n",
       "   'face_token': '5df3deccb38e06baa38ac12af7d8968e'}]}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ec707102",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "376902e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.数据处理 pandas.json_normalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "97662d6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>5df3deccb38e06baa38ac12af7d8968e</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>99.852</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>0.143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>83.177</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>81.705</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>99.999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>3.346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>5.155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>1.008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>38.454</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>1.058892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>12.025846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>0.892258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.391</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.391</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.391</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>0.009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.946</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>90.026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.321</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>173</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          5df3deccb38e06baa38ac12af7d8968e\n",
       "attributes.emotion.sadness                                                     0.004\n",
       "attributes.emotion.neutral                                                    99.852\n",
       "attributes.emotion.disgust                                                       0.0\n",
       "attributes.emotion.anger                                                         0.0\n",
       "attributes.emotion.surprise                                                    0.001\n",
       "attributes.emotion.fear                                                          0.0\n",
       "attributes.emotion.happiness                                                   0.143\n",
       "attributes.beauty.female_score                                                83.177\n",
       "attributes.beauty.male_score                                                  81.705\n",
       "attributes.gender.value                                                       Female\n",
       "attributes.age.value                                                              28\n",
       "attributes.mouthstatus.close                                                  99.999\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    0.001\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              3.346\n",
       "attributes.skinstatus.stain                                                    5.155\n",
       "attributes.skinstatus.acne                                                     1.008\n",
       "attributes.skinstatus.health                                                  38.454\n",
       "attributes.headpose.yaw_angle                                               1.058892\n",
       "attributes.headpose.pitch_angle                                            12.025846\n",
       "attributes.headpose.roll_angle                                              0.892258\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.391\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.391\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.391\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                         0.009\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.054\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                            99.946\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.042\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                  0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                            99.958\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  90.026\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.462\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.944\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.072\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                            -0.321\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.406\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.483\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.955\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                            -0.027\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                            -0.294\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.401\n",
       "face_rectangle.width                                                             173\n",
       "face_rectangle.top                                                                97\n",
       "face_rectangle.left                                                               29\n",
       "face_rectangle.height                                                            173"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4695a128",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ebaf6221",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.人脸检测获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c907e69e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.api账号、通行证\n",
    "API_Key = \"FR1bg4fNm_Vl53EVeHYjOQQTv6_gm_vP\"\n",
    "API_Secret = \"78fJkFZIfdFq5EGrvQ63PGgX5QwddSsw\"\n",
    "\n",
    "# 3.请求方法：POST\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "     \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://img2.baidu.com/it/u=327363434,4196540052&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375\"\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "af6b49ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e7d2e5cf5821e84ca78228f870b02436'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()['faces'][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8ad511ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8ecbde259c6e74aa805f69598db2b0fc'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = face_token\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ac6e3784",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ae7eef84",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "# 2.api账号、通行证\n",
    "API_Key = \"FR1bg4fNm_Vl53EVeHYjOQQTv6_gm_vP\"\n",
    "API_Secret = \"78fJkFZIfdFq5EGrvQ63PGgX5QwddSsw\"\n",
    "\n",
    "# 3.请求方法：POST\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "     \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_token1\":face_token01,\n",
    "    \"face_token2\":face_token02\n",
    "\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d3eb2165",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "432d60ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466272,b9717980-795c-40f3-8d5b-67f81b433d6f',\n",
       " 'time_used': 352,\n",
       " 'confidence': 23.524,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75ef90cc",
   "metadata": {},
   "source": [
    "# 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "095e2bfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_Key = \"FR1bg4fNm_Vl53EVeHYjOQQTv6_gm_vP\"\n",
    "API_Secret = \"78fJkFZIfdFq5EGrvQ63PGgX5QwddSsw\"\n",
    "img_url_01 ='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F8434dd571149b56667991898e2004376212d8267169b3-P2VD0B_fw236&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649382239&t=6f50876f42b564781b9bc484a24600aa'\n",
    "img_url_02 ='https://img2.baidu.com/it/u=327363434,4196540052&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3ca4600f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_Key,API_Secret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    \n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_Key,\n",
    "        'api_secret':API_Secret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    \n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "f8581114",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648469173,ba77023a-c2b3-4cb8-8fd6-b9e0a63929db',\n",
       " 'time_used': 137,\n",
       " 'faces': [{'face_token': '8621550e46ba8af08aa3b99294bbe890',\n",
       "   'face_rectangle': {'top': 97, 'left': 29, 'width': 173, 'height': 173}}],\n",
       " 'image_id': '8a+bXqEezn5PpUuMyXz9xw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_Key,API_Secret,img_url_01)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49a96acb",
   "metadata": {},
   "source": [
    "# 人脸库管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ecc899be",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "# 2.api账号、通行证\n",
    "API_Key = \"FR1bg4fNm_Vl53EVeHYjOQQTv6_gm_vP\"\n",
    "API_Secret = \"78fJkFZIfdFq5EGrvQ63PGgX5QwddSsw\"\n",
    "\n",
    "# 3.请求方法：POST\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "     \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "52bac8a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "b214ef86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '270c40677706faef24e9939fc413dede',\n",
       " 'time_used': 216,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648466632,4fdd287b-2934-4c4c-835f-c3d6c4e71200',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cb24c95",
   "metadata": {},
   "source": [
    "# 人脸识别实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "137e22c7",
   "metadata": {},
   "source": [
    "3. 存入face_token数据\n",
    "4. 获取FaceSet信息数据\n",
    "5. 人脸搜索\n",
    "6. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9533e44a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2f58ea6d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[               字段      类型                                                 说明\n",
       " 0      request_id  String  用于区分每一次请求的唯一的字符串。除非发生404（API_NOT_FOUND ) 或403 ...\n",
       " 1   faceset_token  String                                        FaceSet 的标识\n",
       " 2        outer_id  String                      用户自定义的 FaceSet 标识，如果未定义则返回值为空\n",
       " 3      face_added     Int                     本次操作成功加入 FaceSet的face_token 数量\n",
       " 4      face_count     Int                    操作结束后 FaceSet 中的 face_token 总数量\n",
       " 5  failure_detail   Array  无法被加入 FaceSet 的 face_token 以及原因face_token：人脸标识...\n",
       " 6       time_used     Int  整个请求所花费的时间，单位为毫秒。除非发生404（API_NOT_FOUND )或403 （...\n",
       " 7   error_message  String           当请求失败时才会返回此字符串，具体返回内容见后续错误信息章节。否则此字段不存在。]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<table class=\"wrapped confluenceTable\"><colgroup><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">字段</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">request_id</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">用于区分每一次请求的唯一的字符串。除非发生404（API_NOT_FOUND ) 或403&nbsp;（AUTHORIZATION_ERROR）错误，此字段必定返回。</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">faceset_token</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">FaceSet 的标识</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">用户自定义的 FaceSet 标识，如果未定义则返回值为空</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_added</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">Int</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">本次操作成功加入 FaceSet的face_token 数量</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">face_count</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">操作结束后 FaceSet 中的 face_token 总数量</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">failure_detail</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Array</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">无法被加入 FaceSet 的 face_token 以及原因</span></p><p><span style=\"color: rgb(0,0,0);\">face_token：人脸标识</span></p><p><span style=\"color: rgb(0,0,0);\">reason：不能被添加的原因，包括 INVALID_FACE_TOKEN 人脸表示不存在 ，QUOTA_EXCEEDED 已达到 FaceSet 存储上限</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><p style=\"text-align: justify;\"><span style=\"color: rgb(0,0,0);\">time_used</span></p></td><td colspan=\"1\" class=\"confluenceTd\"><p style=\"text-align: justify;\"><span style=\"color: rgb(0,0,0);\">Int</span></p></td><td colspan=\"1\" class=\"confluenceTd\"><p style=\"text-align: justify;\"><span style=\"color: rgb(0,0,0);\">整个请求所花费的时间，单位为毫秒。除非发生404（API_NOT_FOUND )或403&nbsp;（AUTHORIZATION_ERROR）错误，此字段必定返回。</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><p style=\"text-align: justify;\"><span style=\"color: rgb(0,0,0);\">error_message</span></p></td><td colspan=\"1\" class=\"confluenceTd\"><p style=\"text-align: justify;\"><span style=\"color: rgb(0,0,0);\">String</span></p></td><td colspan=\"1\" class=\"confluenceTd\"><p style=\"text-align: justify;\"><span style=\"color: rgb(0,0,0);\">当请求失败时才会返回此字符串，具体返回内容见后续错误信息章节。否则此字段不存在。</span></p></td></tr></tbody></table>\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "263d68bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "import requests\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "\n",
    "# 2.api账号、通行证\n",
    "API_Key = \"FR1bg4fNm_Vl53EVeHYjOQQTv6_gm_vP\"\n",
    "API_Secret = \"78fJkFZIfdFq5EGrvQ63PGgX5QwddSsw\"\n",
    "\n",
    "# 3.请求方法：POST\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "     \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "52d5919b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 76,\n",
       " 'facesets': [{'faceset_token': '270c40677706faef24e9939fc413dede',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648469083,382e8544-aed1-4873-9bf2-38d3c40f390a'}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbf49c82",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
